﻿namespace SS.WPF.Framework.Restriction
{
    using System;

    public abstract class Restriction<T> : IRestriction
    {
        public virtual bool Validate(T value)
        {
            return true;
        }

        #region Implementation of IRestriction

        public virtual bool Validate(object value)
        {
            if (!IsEnabled)
            {
                return true;
            }

            try
            {
                var data = (T)Convert.ChangeType(value, typeof(T));
                return Validate(data);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public abstract string Description { get; }

        public abstract bool CanParse(string text);

        private bool _isEnabled = true;

        public virtual bool IsEnabled
        {
            get
            {
                return this._isEnabled;
            }
            set
            {
                this._isEnabled = value;
            }
        }

        #endregion
    }
}